from elasticsearch import Elasticsearch

ontologies = {}

def read_ontologies(filename):
    ontologies_file = open(filename, 'r')

    # Skip ontologies table headers
    ontologies_file.readline()

    line = ontologies_file.readline()
    while line != "":
        line = line.strip()
        ontology_property = line.split('\t')
        ontology_id = ontology_property[0]
        ontology_vid = ontology_property[1]
        ontologies[ontology_id] = ontology_vid

        line = ontologies_file.readline()

    ontologies_file.close()

def resolve_tag(string):
    tag_property = string.split('\t')
    tag = {
            'id': tag_property[0],
            'name': tag_property[1],
            'fid': tag_property[3],
            'ontology': ontologies[tag_property[4]]
    }

    return tag

def import_tag(es, tag):
    doc = {
            "@id": tag['fid'],
            "name": tag['name'],
            "ontology": tag['ontology']
    }

    res = es.index(index='mitagger', doc_type='tag', id = tag['id'], body = doc)
    return res['created']

def import_tags():
    es = Elasticsearch()
    tags_file = open('tags.dat', 'r')

    # Skip tag table headers 
    tags_file.readline()

    line = tags_file.readline()
    count = 0
    while line != '':
        tag = resolve_tag(line.strip())
        if (import_tag(es, tag) == False):
            print 'Import Error', tag
            break

        line = tags_file.readline()
        count = count + 1

    print(count)
    tags_file.close()

read_ontologies('ontologies.dat')
import_tags()
